<?php
#### TABLE #####################################################################
class Table {

	var $output;

	#### OPEN FORM #############################################################
	function open($id = NULL, $width = '100%', $align =  NULL) {
		if ($id) $id = 'id="'.$id.'" ';
		if ($width) $width = 'width="'.$width.'" ';
		if ($align) $align = 'align="'.$align.'" ';

		$this->output ='<table '.$id.$width.$align.'class="styled">';
	}

	#### TABLE ROW #############################################################
	function tr($cells, $id = NULL, $class = NULL, $style = NULL) {
		if ($id) $id = ' id="'.$id.'"';
		if ($class) $class = ' class="'.$class.'"';
		if ($style) $style = ' style="'.$style.'"';

		$this->output .= '<tr'.$class.$id.$style.'>'.$cells.'</tr>';
	}

	#### TABLE ROW GRID ########################################################
	function tr_grid($td_array, $columns) {

		// Set up a few vars
		$td_content = NULL;
		$td_count = 0;
		$loop_count = 0;
		$dummy_array = $td_array;

		if ($dummy_array) foreach ($dummy_array as $dummy) {

			$td_content .= $td_array[$loop_count];

			$td_count++;
			$loop_count++;

			// If we have $grid_width TD's put them in a TR of if we have reached
			// the end of our array then drop the TD's out into a TR regardless
			// of if there are enough to make up the $grid_width
			if ($td_count == $columns || $loop_count == count($dummy_array)) {
				$this->tr($td_content);
				$td_count = 0;
				$td_content = NULL;
			}
		}
	}

	#### TABLE HEADER ##########################################################
	function th($output, $size = NULL, $center = FALSE, $colspan = NULL) {

		// What type of size do we have, % or not?
		if (strstr($size, '%')) $size = ' style="width: '.$size.'"';
		else $size = ' style="width: '.$size.'px"';

		if ($colspan != NULL) $colspan = ' colspan="'.$colspan.'"';
		if ($center != NULL) $center = ' align="center"';

		return '<th'.$size.$center.$colspan.'>'.$output.'</th>';
	}

	#### TABLE CELL ############################################################
	function td($output, $center = FALSE, $colspan = NULL, $class = NULL, $style = NULL) {
		if ($colspan != NULL) $colspan = ' colspan="'.$colspan.'"';
		if ($center != NULL) $center = ' align="center"';
		if ($class != NULL) $class = ' class="'.$class.'"';
		if ($style) $style = ' style="'.$style.'"';

		return '<td'.$colspan.$center.$class.$style.'>'.$output.'</td>';
	}

	#### CLOSE FORM ############################################################
	function close() {
		$this->output .= '</table>';
	}

	#### DRAG AND ##############################################################
	function drag_drop($table_id, $process_url) {
		return '<script type="text/javascript">
			$(document).ready(function() {
			    // Initialise the table
			    $("'.$table_id.'").tableDnD({
				    onDrop: function(table, row) {
	            		$("'.$table_id.'_debug").load("'.$process_url.'", $.tableDnD.serialize());
	       			}
       			});
			});
		</script>';
	}

	#### OUTPUT ################################################################
	function output() {
		return $this->output;
	}

}
?>